package o;

import android.annotation.TargetApi;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.os.Build.VERSION;
import android.service.notification.StatusBarNotification;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.huawei.appgallery.foundation.application.pkgmanage.model.update.ApkUpgradeInfo;
import com.huawei.appgallery.serverreqkit.api.bean.ResponseBean;
import com.huawei.appgallery.serverreqkit.api.listener.IServerCallBack;
import com.huawei.appgallery.updatemanager.api.UpgradeResponse;
import com.huawei.appgallery.updatemanager.impl.store.bean.UpgradeRequest;
import com.huawei.hmf.annotation.ApiDefine;
import com.huawei.hms.support.hianalytics.HiAnalyticsConstant;
import java.util.ArrayList;
import java.util.List;
import o.czk.g;
import o.dge.d;

@ApiDefine(uri = czl.class)
public class daa implements czl {
    public czo ˎ(@NonNull Context context, boolean z, int i) {
        return ˋ(context, z, i);
    }

    public void ˎ(Context context, String str, IServerCallBack iServerCallBack, int i) {
        cvt ˋ;
        if (TextUtils.isEmpty(str)) {
            ˋ = UpgradeRequest.ˋ(context, ((cdg) cdb.ॱ(cdg.class)).ʻ(), false, i);
        } else {
            ˋ = UpgradeRequest.ˏ(context, str, i);
        }
        cfw.ˎ(ˋ, iServerCallBack);
    }

    public ApkUpgradeInfo ॱ(@NonNull Context context, @NonNull String str, int i, int i2) {
        PackageInfo ᐝ = ((cdg) cdb.ॱ(cdg.class)).ᐝ(str);
        if (ᐝ == null) {
            czi.ˎ.ˊ("UpdateCheckImpl", "get app packageInfo failed,packageName:" + str);
            return null;
        }
        ApkUpgradeInfo ˋ;
        czi.ˎ.ˋ("UpdateCheckImpl", "get update pkg: " + str + ", version:" + ᐝ.versionCode);
        List arrayList = new ArrayList();
        arrayList.add(ᐝ);
        cvt ˋ2 = UpgradeRequest.ˋ(context, arrayList, true, i2);
        ˋ2.ˊ(i);
        ResponseBean ॱ = cfw.ॱ(ˋ2);
        if (ॱ.getResponseCode() == 0 && ॱ.getRtnCode_() == 0) {
            boolean z;
            UpgradeResponse upgradeResponse = (UpgradeResponse) ॱ;
            List ॱ2 = upgradeResponse.ॱ();
            List ˎ = upgradeResponse.ˎ();
            ॱ(upgradeResponse);
            ˋ = ˋ(context, str, ᐝ, ॱ2, true);
            if (ˋ == null) {
                ˋ = ˋ(context, str, ᐝ, ˎ, false);
                z = false;
            } else {
                z = true;
            }
            if (ˋ != null) {
                ˋ(context, false, z);
            }
        } else {
            czi.ˎ.ˊ("UpdateCheckImpl", "get app update msg failed,responseCode is " + ॱ.getResponseCode());
            ˋ = null;
        }
        String str2 = "00|" + str;
        if (ˋ != null) {
            str2 = "01|" + str;
            czi.ˎ.ˋ("UpdateCheckImpl", "update pkg info: " + str + ", version:" + ˋ.ʿ());
        }
        dgh.onEvent(new d(context, g.ʽ).ˏ(str2).ˋ());
        return ˋ;
    }

    @Nullable
    public ApkUpgradeInfo ˏ(@NonNull Context context, @NonNull String str, int i, int i2, int i3) {
        czi.ˎ.ˋ("UpdateCheckImpl", "get update pkg with minVersionCode: " + str + ", version:" + i);
        PackageInfo packageInfo = new PackageInfo();
        packageInfo.packageName = str;
        packageInfo.versionCode = i;
        ApplicationInfo applicationInfo = new ApplicationInfo();
        applicationInfo.targetSdkVersion = i2;
        packageInfo.applicationInfo = applicationInfo;
        List arrayList = new ArrayList();
        arrayList.add(packageInfo);
        ResponseBean ॱ = cfw.ॱ(UpgradeRequest.ˋ(context, arrayList, false, i3));
        if (ॱ.getResponseCode() == 0 && ॱ.getRtnCode_() == 0) {
            return ˏ(str, packageInfo, ((UpgradeResponse) ॱ).ॱ());
        }
        return null;
    }

    private static ApkUpgradeInfo ˏ(String str, PackageInfo packageInfo, List<ApkUpgradeInfo> list) {
        if (list == null || packageInfo == null) {
            return null;
        }
        for (ApkUpgradeInfo apkUpgradeInfo : list) {
            if (apkUpgradeInfo.ᐝ().equals(str)) {
                return apkUpgradeInfo;
            }
        }
        return null;
    }

    private czo ˋ(Context context, boolean z, int i) {
        czi.ˎ.ˋ("UpdateCheckImpl", "searchUpdatableApk()");
        czo o_czo = new czo();
        UpgradeResponse upgradeResponse = (UpgradeResponse) cfw.ॱ(UpgradeRequest.ˋ(context, ((cdg) cdb.ॱ(cdg.class)).ʻ(), false, i));
        o_czo.ˎ(upgradeResponse.getResponseCode());
        o_czo.ˋ(upgradeResponse.getRtnCode_());
        if (upgradeResponse.getResponseCode() == 0 && upgradeResponse.getRtnCode_() == 0) {
            ((czp) dbn.ˎ(czp.class)).ˎ(context, System.currentTimeMillis());
            ॱ(upgradeResponse);
            boolean ˋ = ˋ(context, upgradeResponse);
            ˋ(upgradeResponse);
            ˋ(context, z, ˋ);
            Logger.ˎ("UpdateCheckImpl", "get UpdateInterval = " + upgradeResponse.ʻ());
            daj.ˎ().ˋ(upgradeResponse.ʻ());
            ˋ = ((czp) dbn.ˎ(czp.class)).ˏ(context);
            czu o_czu = (czu) dbn.ˎ(czu.class);
            dgh.onEvent(new d(context, g.ʻ).ˏ(o_czu.ˋ(false, 1) + HiAnalyticsConstant.REPORT_VAL_SEPARATOR + o_czu.ʻ(false, 1) + HiAnalyticsConstant.REPORT_VAL_SEPARATOR + ((czu) dbn.ˎ(czu.class)).ˏ(1) + HiAnalyticsConstant.REPORT_VAL_SEPARATOR + (ˋ ? 1 : 0)).ˋ());
        }
        return o_czo;
    }

    public void ˋ(Context context, boolean z, boolean z2) {
        ˊ(context, z, z2);
    }

    public static void ˊ(Context context, boolean z, boolean z2) {
        int ˋ = ((czu) dbn.ˎ(czu.class)).ˋ(true, 1);
        int ʻ = ((czu) dbn.ˎ(czu.class)).ʻ(true, 1);
        int i = ˋ + ʻ;
        int ˏ = ((czu) dbn.ˎ(czu.class)).ˏ(1);
        if (z2 && ˋ > 0) {
            czi.ˎ.ˋ("UpdateCheckImpl", "new recommend update has appeared!");
            dad.ˎ(i);
        }
        if (ˋ == 0) {
            dad.ॱ();
            czi.ˎ.ˋ("UpdateCheckImpl", "RECOMMEND_UPDATABLE_APP size is 0.so cancel the update_notify_id.");
            cei.ˋ(context, "UpdateManager", 1020);
        }
        if (VERSION.SDK_INT >= 23) {
            ˊ(context, ˋ);
        }
        ˏ(ˋ, ʻ, i, ˏ);
        int ˋ2 = dak.ˏ().ˋ();
        if (ˋ != 0) {
            ˏ = ˋ + ʻ;
        } else {
            ˏ = ˋ;
        }
        if (ˋ2 != ˏ) {
            dak.ˏ().ˎ();
            dak.ˏ().ˎ(ˏ);
            daf.ˎ(ContentManager.getInstant().getContext(), ˏ);
        } else if (z) {
            daf.ˎ(ContentManager.getInstant().getContext(), ˏ);
        }
        dad.ˏ(i);
        ApkUpgradeInfo ॱ = ((czu) dbn.ˎ(czu.class)).ॱ(context.getPackageName(), false, 0);
        if (ॱ != null) {
            dad.ˋ(ॱ.ʽ());
        }
        Intent intent = new Intent(ccv.ˋ());
        intent.setPackage(context.getPackageName());
        context.sendBroadcast(intent);
    }

    private static void ˏ(int i, int i2, int i3, int i4) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("ApkObtainTask.ACTION_UPDATABLE_APK total update size:").append(i3).append(",recommend size:").append(i).append(",not recommend size:").append(i2).append(",ignore size:").append(i4);
        czi.ˎ.ˋ("UpdateCheckImpl", stringBuilder.toString());
    }

    private static void ॱ(UpgradeResponse upgradeResponse) {
        if (daj.ˎ().ˋ()) {
            daj.ˎ().ॱ(upgradeResponse.ˏ());
        }
        daj.ˎ().ˊ(upgradeResponse.ˊ());
        daj.ˎ().ॱ(upgradeResponse.ᐝ());
        daj.ˎ().ˏ(upgradeResponse.ˋ());
    }

    private static boolean ˋ(Context context, UpgradeResponse upgradeResponse) {
        boolean z;
        List arrayList = new ArrayList();
        StringBuilder stringBuilder = new StringBuilder();
        if (upgradeResponse.ॱ() != null) {
            czu o_czu = (czu) dbn.ˎ(czu.class);
            z = false;
            for (ApkUpgradeInfo ˎ : upgradeResponse.ॱ()) {
                ApkUpgradeInfo ˎ2 = o_czu.ˎ(ˎ);
                if (ˎ2 != null) {
                    boolean z2;
                    if (ˎ2.ʻॱ() <= 0) {
                        stringBuilder.append(ˎ2.ᐝ());
                    }
                    if (z) {
                        z2 = z;
                    } else {
                        z2 = ˊ(ˎ2.ᐝ(), true, ˎ2);
                    }
                    arrayList.add(ˎ2);
                    z = z2;
                }
            }
        } else {
            z = false;
        }
        Object stringBuilder2 = stringBuilder.toString();
        if (!TextUtils.isEmpty(stringBuilder2)) {
            czi.ˎ.ˊ("UpdateCheckImpl", "online update size empty:" + stringBuilder2);
        }
        czi.ˎ.ˋ("UpdateCheckImpl", "searchUpdatableApk completed, RECOMMEND_UPDATABLE_APP size:" + arrayList.size());
        ((czu) dbn.ˎ(czu.class)).ˏ(context, arrayList, true);
        return z;
    }

    private static boolean ˊ(String str, boolean z, ApkUpgradeInfo apkUpgradeInfo) {
        if (z && !((cdg) cdb.ॱ(cdg.class)).ʼ(str)) {
            czu o_czu = (czu) dbn.ˎ(czu.class);
            List arrayList = new ArrayList(1);
            arrayList.add(apkUpgradeInfo);
            o_czu.ˏ(arrayList);
            if (arrayList.size() == 1) {
                ApkUpgradeInfo ॱ = o_czu.ॱ(str, true, 0);
                if (ॱ == null || ॱ.ʿ() != apkUpgradeInfo.ʿ()) {
                    return true;
                }
            }
        }
        return false;
    }

    private static void ˋ(UpgradeResponse upgradeResponse) {
        List arrayList = new ArrayList();
        if (upgradeResponse.ˎ() != null) {
            czu o_czu = (czu) dbn.ˎ(czu.class);
            for (ApkUpgradeInfo ˎ : upgradeResponse.ˎ()) {
                ApkUpgradeInfo ˎ2 = o_czu.ˎ(ˎ2);
                if (ˎ2 != null) {
                    arrayList.add(ˎ2);
                }
            }
        }
        czi.ˎ.ˋ("UpdateCheckImpl", "searchUpdatableApk completed, NOT_RECOMMEND_UPDATABLE_APP size:" + arrayList.size());
        ((czu) dbn.ˎ(czu.class)).ˊ(arrayList, true);
    }

    @TargetApi(23)
    private static void ˊ(Context context, int i) {
        try {
            int i2;
            for (StatusBarNotification statusBarNotification : ((NotificationManager) context.getSystemService("notification")).getActiveNotifications()) {
                if (1020 == statusBarNotification.getId()) {
                    i2 = statusBarNotification.getNotification().extras.getInt("update_notify_app_size", -1);
                    if (i2 != -1 && i != i2) {
                        cei.ˋ(context, "UpdateManager", 1020);
                        return;
                    }
                    return;
                }
            }
        } catch (NullPointerException e) {
            Logger.e("UpdateCheckImpl", "NullPointerException");
        } catch (Exception e2) {
            Logger.e("UpdateCheckImpl", "Exception" + e2.getMessage());
        }
    }

    private static ApkUpgradeInfo ˋ(Context context, String str, PackageInfo packageInfo, List<ApkUpgradeInfo> list, boolean z) {
        if (list == null || packageInfo == null || packageInfo.applicationInfo == null) {
            return null;
        }
        czu o_czu = (czu) dbn.ˎ(czu.class);
        for (ApkUpgradeInfo apkUpgradeInfo : list) {
            ApkUpgradeInfo apkUpgradeInfo2;
            if (apkUpgradeInfo2.ᐝ().equals(str)) {
                apkUpgradeInfo2 = o_czu.ˎ(apkUpgradeInfo2);
                if (apkUpgradeInfo2 != null) {
                    if (z) {
                        o_czu.ˊ(context, apkUpgradeInfo2, true);
                    } else {
                        o_czu.ˋ(apkUpgradeInfo2, true);
                    }
                }
                return apkUpgradeInfo2;
            }
        }
        return null;
    }
}
